// 重写数组函数 
// 获取数组原型链上的方法
let oldArrayProtoMethods = Array.prototype;
// 创建劫持的对象
let arrMethods = Object.create(oldArrayProtoMethods)

let methods = [
  'push',
  'pop',
  'shift',
  'unshift',
  'splice',
  'sort',
  'reverse'
]

methods.forEach(method => {
  arrMethods[method] = function (...args) {
  	console.log('args at line 18 in observe/arr.js:', args)

    let result = oldArrayProtoMethods[method].apply(this, args)

    let inserted
    switch (method) {
      case "push":
      case "unshift":
        inserted = args;
        break;

      case "splice":
        inserted = args.slice(2)

        break;
    }

    const ob = this.__ob__ 
    if(inserted && inserted.length){
      ob.observeArray(inserted)
    }
    

    return result
  }
})


export default arrMethods

